<?php
namespace app\api\controller;
use think\Db;
use app\admin\model\Config;


class Major extends App
{
    /*public function index()
    {
        
        $education_level= $this->request->get("education_level",'','trim'); 
        $cate_id= $this->request->get("cate_id",0,'intval'); 
		
		
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		if(empty($education_level)){
			$education_level = $education_level_list[0];
		}
		$cate_list = Db::name("major")->where('cate_level',1)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		if(empty($cate_id)){
			$major_list = $cate_list;
		}else{
			$major_list = Db::name("major")->where('cate_level',1)->where('id',$cate_id)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		}
		foreach($major_list as $k=>$v){
			$sub1 = Db::name("major")->where('cate_level',2)->where('cate_pid',$v['id'])->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
			foreach($sub1 as $k1=>$v1){
				$sub1[$k1]['sub'] = Db::name("major")->where('cate_level',3)->where('cate_pid',$v1['id'])->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
			}	
			$major_list[$k]['sub'] = $sub1;
		}
		//dump($cate_list);die;
        $data = array('education_level_list'=>$education_level_list,'cate_list'=>$cate_list,'major_list'=>$major_list);
		return $this->show(1,"成功", $data);
		
    }
    
    public function category()
    {
        
        $education_level= $this->request->get("education_level",'','trim'); 
        $cate_id= $this->request->get("cate_id",0,'intval'); 
        if(empty($cate_id)){
            return $this->show(2001,"缺少参数二级分类id!", '');
        }
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		if(empty($education_level)){
			$education_level = $education_level_list[0];
		}
		$cate_list = Db::name("major")->where('cate_level',1)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		foreach($cate_list as $k=>$v){
			$sub = array_map(function ($rs){
				$rs['sub_count'] = Db::name("major")->where('cate_level',3)->where('cate_pid',$rs['id'])->count();
				return $rs;
			}, Db::name("major")->where('cate_level',2)->where('cate_pid',$v['id'])->field('id,cate_title,major_code')->order('sort asc,id asc')->select());		
			
			$cate_list[$k]['sub'] = $sub;
			$cate_list[$k]['sub_count'] = count($sub);
		}
		$major_list = Db::name("major")->where('cate_level',3)->where('cate_pid',$cate_id)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		
		//dump($cate_list);die;
        $data = array('education_level_list'=>$education_level_list,'cate_list'=>$cate_list,'major_list'=>$major_list);
		return $this->show(1,"成功", $data);
		
    }*/
    
    public function index()
    {
        
        $education_level= $this->request->get("education_level",'','trim'); 
        $cate_id= $this->request->get("cate_id",0,'intval'); 
		
		
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		if(empty($education_level)){
			$education_level = $education_level_list[0];
		}
		$cate_list = Db::name("major")->where('cate_level',1)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		if(empty($cate_id)){
			$major_list = $cate_list;
		}else{
			$major_list = Db::name("major")->where('cate_level',1)->where('id',$cate_id)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		}
		foreach($major_list as $k=>$v){
			$sub1 = Db::name("major")->where('cate_level',2)->where('cate_pid',$v['id'])->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
			foreach($sub1 as $k1=>$v1){
				$sub1[$k1]['sub'] = Db::name("major")->where('cate_level',3)->where('cate_pid',$v1['id'])->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
			}	
			$major_list[$k]['sub'] = $sub1;
		}
		
       // $data = array('education_level_list'=>$education_level_list,'cate_list'=>$cate_list,'major_list'=>$major_list);
		return $this->show(1,"成功", $major_list);
		
    }
	
	/*
	//专业库-筛选条件
	public function get_major_condition()
    {
        
        $education_level= $this->request->get("education_level",'','trim'); 
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		if(empty($education_level)){
			$education_level = $education_level_list[0];
		}
		$cate_list = Db::name("major")->where('cate_level',1)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
	
		//dump($cate_list);die;
        $data = array('education_level_list'=>$education_level_list,'cate_list'=>$cate_list);
		return $this->show(1,"成功", $data);
		
    }
	*/
	
    public function get_major_education_list()
    {
        
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
		return $this->show(1,"成功", $education_level_list);
		
    }
	
	public function get_major_cate_one_list()
    {
        
        $education_level= $this->request->get("education_level",'','trim'); 
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		if(empty($education_level)){
			$education_level = $education_level_list[0];
		}
		$cate_list = Db::name("major")->where('cate_level',1)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
	
		//dump($cate_list);die;
        //$data = array('education_level_list'=>$education_level_list,'cate_list'=>$cate_list);
		return $this->show(1,"成功", $cate_list);
		
    }
	
	public function get_major_cate_two_list()
    {
        
        $education_level= $this->request->get("education_level",'','trim'); 
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		if(empty($education_level)){
			$education_level = $education_level_list[0];
		}
		$cate_list = Db::name("major")->where('cate_level',1)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		foreach($cate_list as $k=>$v){
			$sub = array_map(function ($rs){
				$rs['sub_count'] = Db::name("major")->where('cate_level',3)->where('cate_pid',$rs['id'])->count();
				return $rs;
			}, Db::name("major")->where('cate_level',2)->where('cate_pid',$v['id'])->field('id,cate_title,major_code')->order('sort asc,id asc')->select());		
			
			$cate_list[$k]['sub'] = $sub;
			$cate_list[$k]['sub_count'] = count($sub);
		}
		
		return $this->show(1,"成功", $cate_list);
		
    }
	
    public function category()
    {
        
        
        $cate_id= $this->request->get("cate_id",0,'intval'); 
        if(empty($cate_id)){
            return $this->show(2001,"缺少参数二级分类id!", '');
        }
		$major_list = Db::name("major")->where('cate_level',3)->where('cate_pid',$cate_id)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		
		return $this->show(1,"成功", $major_list);
		
    }
	
    public function detail()
    {
        
        $major_id = $this->request->get("major_id",0,'intval'); 
        if(empty($major_id)){
            return $this->show(2001,"缺少参数major_id!", '');
        }
        $info = Db::name("major")->field('id,major_name,major_code,major_introduction,male_female_ratio,education_level,years,xuewei,purpose,requirement,subject_requirement,knowledge,kaoyan_direction,zhuyao_courses,famous_people')->where('id',$major_id)->where('cate_level',3)->find();
        if(empty($info)){
            return $this->show(2001,"专业不存在!", '');
        }
		$info['is_collect'] = 0;
        $user_id = $this->user_id;
		if(!empty($user_id)){
			$flag = Db::name("major_collect")->where('user_id',$user_id)->where('major_id',$major_id)->value('id');
			if(!empty($flag)){
				$info['is_collect'] = 1;
			}
		}
		return $this->show(1,"成功", $info);
		
    }
    
}